//
//  MTClubRegExprPattern.swift
//  MT
//
//  Created by ooops on 2019/12/27.
//  Copyright © 2019 张强. All rights reserved.
//

import Foundation
import Eureka

public enum MTClubRegExprPattern: String {
//    case RealNameRule = "^[^-负~`!@#$%^&*()_+=|\"\"\\'':;><,.?/,、\\}\\{\\[\\]。？}{a-zA-Z]|[0-9\\u4e00-\\u9fa5]{2,15}"
//    case RealNameIDCardNumbre = "^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$"
//    case BankName = "^[\\u4e00-\\u9fa5]{1,}$"
//    case BankCardNo = "^([1-9]{1})(\\d{15}|\\d{18})$"
//    //case BankOpen = "^((?!请输入开户行名称).)*$"
//    case BankOpen = "^[\\u4e00-\\u9fa5]{4,}$"
//    case BankCardOwner = "^[\\u4e00-\\u9fa5]{2,}$"
//    case CashNowMoney = "^(([1-9][0-9]*)|(([0]\\.\\d{1,2}|[1-9][0-9]*\\.\\d{1,2})))$"
    
    case clubName = "^[\\u4e00-\\u9fffa-zA-Z]{1,10}$"// [\\u4e00-\\u9fa5] //[\\u4e00-\\u9fa5]
    /// 人数限制
    case clubPeoples = "^(?:[1-9]?\\d|1000)$"
    /// 非空判断
    case clubNotNull = "^{1,}$"// [\\u4e00-\\u9fa5]
    
    case clubExNotNull = "^[\\s\\S]*.*[^\\s][\\s\\S]*$"
}

open class MTClubExp: RuleType {

    public var regExpr: String = ""
    public var id: String?
    public var validationError: ValidationError
    public var allowsEmpty = true
    
    public init(regExpr: MTClubRegExprPattern, allowsEmpty: Bool = true, msg: String = "Invalid field value!", id: String? = nil) {
        self.validationError = ValidationError(msg: msg)
        self.regExpr = regExpr.rawValue
        self.allowsEmpty = allowsEmpty
        self.id = id
    }
    
    public func isValid(value: String?) -> ValidationError? {
        if let value = value, !value.isEmpty {
            let predicate = NSPredicate(format: "SELF MATCHES %@", regExpr)
            guard predicate.evaluate(with: value) else {
                return validationError
            }
            return nil
        } else if !allowsEmpty {
            return validationError
        }
        return nil
    }
    
}
